package cn.edu.nuc.ssm.controller.sys;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.domain.Role;
import cn.edu.nuc.ssm.domain.Sysfunction;
import cn.edu.nuc.ssm.service.interfaces.SysRoleService;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
	private SysRoleService sysroleservice;
	/**
	 * 通过servlet进入添加角色页面。
	 * @return
	 */
    @RequestMapping(value="add",method=RequestMethod.GET)
    public String getAdd(){
    	return "sys/role/add";
    }
    /**
     * 添加角色（post）
     * @param role
     * @return
     */
    @RequestMapping(value="add",method=RequestMethod.POST)
    public String addRole(Role role){
    	int result = sysroleservice.insertRole(role);
    	if(result == 1){
    		return "redirect:list";
    	}
    	return "error";
    } 
	/**
	 * 进入角色列表(查询)
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String getFun(@RequestParam(name="rolename",defaultValue="") String rolename,
			@RequestParam(name="current",defaultValue="1") int current , 
			Model model){
		Pager<Role> pager = sysroleservice.pageList(rolename, current);
		model.addAttribute("pager", pager);
		return "sys/role/list";
	}
	
	/**
	 * 更新，（全部进行更新）
	 */
	//获取功能信息
	@RequestMapping(value="detail/{roleid}",method=RequestMethod.GET)
	public String updateRole(@PathVariable("roleid") Integer roleid,Model model){
		Role role = sysroleservice.selectByRoleid(roleid);
		model.addAttribute("role", role);
		return "sys/role/update";
	}
	//更新
	@RequestMapping(value="update/{roleid}",method=RequestMethod.PUT)
	public String updateRole(@PathVariable("roleid") Integer roleid,Role role){
		int result = sysroleservice.updateByPrimaryKey(role);
		if(result == 1){
			return "redirect:/role/list";	
		}
		return  "redirect:error";
	}
	//删除单个角色信息。
	@RequestMapping(value="delete/{roleid}",method=RequestMethod.DELETE)
	public String deleteFun(@PathVariable("roleid") Integer roleid){
		int result = sysroleservice.deleteByPrimaryKey(roleid);
		if(result==1){
			return "redirect:/role/list";	
		}
		return  "error";
	}
	
	
	
	
	//为角色添加权限
	/**
	 * 初始化角色权限
	 * @param roleid
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/initFunction",method=RequestMethod.GET)
	public String initFunction( int roleid,Model model){
		
		//调用获取角色详细信息的方法（主要用来显示rolename以及通过roleid来进行编辑role的功能）
		Role role = sysroleservice.selectByRoleid(roleid);
		//获取角色的权限
		List<Sysfunction> list = sysroleservice.initfunction(role);
		System.out.println(role.getRolename() +"有功能"+ list.size());
		//将获取的角色功能存放到list集合里进行提交到jsp界面
		model.addAttribute("list", list);
		//返回role的信息
		model.addAttribute("role", role);
		return "sys/role/right";
	}
	/**
	 * 保存角色权限
	 * @param roleid
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/saveright",method=RequestMethod.POST)
	public String saveright( int roleid, HttpServletRequest request){
		//得到功能id
		String[] funids = request.getParameterValues("ckrr");
		System.out.println(funids);
		//删除权限
        sysroleservice.deleteRight(roleid);
		//重新增加权限
		int count1 = sysroleservice.saveright(roleid,funids);
		if(count1 > 0){	
			return "redirect:list";
		}	
		request.setAttribute("msg", "保存角色权限失败");
		return "sys/role/right";		
	}
}
